package com.zq.zhufengfm;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.handmark.pulltorefresh.library.ILoadingLayout;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.squareup.picasso.Picasso;
import com.zq.zhufengfm.adapter.TrackItemAdapter;
import com.zq.zhufengfm.entity.Tracks;
import com.zq.zhufengfm.entity.album.AlbumDetail;
import com.zq.zhufengfm.tasks.TaskCallback;
import com.zq.zhufengfm.tasks.TasklResult;
import com.zq.zhufengfm.tasks.albumDetail.AlbumDetailTask;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 当点击项目列表时  展示该activity 并播放  电台声音
 */
public class AlbumDetailActivity extends AppCompatActivity implements TaskCallback, AdapterView.OnItemClickListener {

    private String albumId;

    private AlbumDetail albumDetail;//数据


    private PullToRefreshListView listView;
    //listView中的数据
    private TrackItemAdapter adapter;
    private ArrayList<Tracks> trackList;

    @ViewInject(R.id.album_detail_title)
    private TextView albumTitle;

    @ViewInject(R.id.album_detail_albumName)
    private TextView albumName;

    @ViewInject(R.id.album_detail_intro)
    private TextView albumIntroduce;

    @ViewInject(R.id.album_detail_pic)
    private ImageView albumPic;

    @ViewInject(R.id.album_detail_image_handIcon)
    private ImageView albumHandIcon;

    @ViewInject(R.id.album_detail_imageback)
    private ImageView imgBack;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_album_detail);

        ViewUtils.inject(this);

        imgBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {finish();}});

        listView = (PullToRefreshListView) findViewById(R.id.album_activity_listView);

        trackList = new ArrayList<Tracks>();
        //获取地址：
        albumId = getIntent().getStringExtra(Constants.START_ALBUMDETAIL_ALBUMID_VAR);

        //初始化下拉刷新组件：
        initPtr2();

        //设置单击事件：
        listView.setOnItemClickListener(this);

    }

    //初始化下拉刷新组件：
    private void initPtr2() {

        //TODO :暂时  未实现下拉刷新功能：
        //获取下拉刷新布局
        ILoadingLayout proxy = listView.getLoadingLayoutProxy(true,false);

        listView.setMode(PullToRefreshBase.Mode.PULL_FROM_END);

    }

    //每次进入  专辑详情都需要进行刷新    在此进行网络加载数据：
    @Override
    protected void onResume() {
        super.onResume();

        //创建异步任务解析网址：
        AlbumDetailTask task = new AlbumDetailTask(this);

        task.execute(albumId);//将参数id传过去：
    }

    @Override
    public void onTaskFinished(TasklResult result) {

        if(result != null) {
            //处理数据：
            if (result.action == Constants.TASK_ACTION_ALBUMDETAIL) {//判断标识
                if (result.resultCode == Constants.TASK_RESULT_OK) {//判断  返回码 是否成功
                    //开始解析数据
                    if(result.data!=null){
                        JSONObject json = (JSONObject) result.data;

                        try {

                            //解析专辑对象：
                            JSONObject albumjson = json.getJSONObject("album");

                            TypeToken<AlbumDetail> token = new TypeToken<AlbumDetail>(){};
                            Gson gson = new Gson();
                            AlbumDetail albumDetail = gson.fromJson(albumjson.toString(), token.getType());

                            albumTitle.setText(albumDetail.getTitle());
                            albumName.setText(albumDetail.getNickname());

                            albumIntroduce.setText(albumDetail.getIntro());


                            Picasso.with(this).load(albumDetail.getAvatarPath()).into(albumHandIcon);

                            Picasso.with(this).load(albumDetail.getCoverMiddle()).into(albumPic);


                            //TODO: tags未适配

                            //获取track数据   曲目信息 并适配
                            JSONObject trackJson = json.getJSONObject("tracks");
                            JSONArray trackArrays = trackJson.getJSONArray("list");

                            TypeToken<List<Tracks>> to = new TypeToken<List<Tracks>>(){};
                            Gson g = new Gson();
                            trackList = g.fromJson(trackArrays.toString(),to.getType());

                            adapter = new TrackItemAdapter(this,trackList);

                            listView.setAdapter(adapter);

                            adapter.notifyDataSetChanged();
                           //获取数据
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }else{

        }
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        //获取一个新的Track对象：当点击某一项时  打开另外一个新的 页面  开始播放电台：
        Intent intent = new Intent(this,TrackDetailActivity.class);

        //传入所有的数据集合：
        intent.putParcelableArrayListExtra(Constants.SERVICE_PLAY_LIST,trackList);
        //传入被点击的position
        intent.putExtra(Constants.CLICK_TRACKS_POSITION, position-1);

        startActivity(intent);
    }

}
